﻿using UnityEngine;
using System.Collections;
using System.IO;
using System.Text;
using System;

public class TeachFileStream : MonoBehaviour {

	// Use this for initialization
	void Start () {
        //写入
        string text = "中国是世界上人口第一大国。中国是世界上最幸福的国家之一。";
        FileStream stream = new FileStream(@"D:\Data.txt", FileMode.Create, FileAccess.ReadWrite);

        //byte[] first = Encoding.UTF8.GetBytes("中国是世界上人口第一大国。");

        //byte[] buffer = Encoding.UTF8.GetBytes(text);

        //int length = buffer.Length - first.Length;
        //stream.Write(buffer, first.Length, length);

        //stream.WriteByte(Convert.ToByte(1));
        //stream.WriteByte(Convert.ToByte(2));
        //stream.WriteByte(Convert.ToByte(3));

        //StreamWriter writer = new StreamWriter(stream);
        //writer.Write(text);
        //writer.Flush();
        //byte[] first = Encoding.UTF8.GetBytes("中国是世界上人口第一大国。");

        //int lenght = ((int)stream.Length) - first.Length;
        //byte[] buffer = new byte[stream.Length];

        //Debug.Log(stream.Position);//流的指针位置
        //Debug.Log(stream.Length);//流的总长度
        //stream.Seek(first.Length, SeekOrigin.Begin);//Seek可以修改Position位置

        //stream.Read(buffer,0,first.Length);//读取第一句话
        //stream.Read(buffer, first.Length, lenght);//读取第二句话，
        ////buffer是写入的数组，offset是针对buffer的位置，length是写入长度

        //Debug.Log(stream.Position);
        //Debug.Log(Encoding.UTF8.GetString(buffer));

        //stream.Close();

        //读取
        //FileStream stream = new FileStream(@"D:\Data.txt", FileMode.Open, FileAccess.Read);
        ////StreamReader reader = new StreamReader(stream);
        ////string buffer = reader.ReadToEnd();

        //byte[] array = new byte[stream.Length];
        //stream.Read(array,0, array.Length);
        //string result = Encoding.UTF8.GetString(array,0,array.Length);

        ////Debug.Log(buffer);
        //Debug.Log(result);
        stream.Close();

    }

}
